package com.controller.stu;

import java.io.IOException;

import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AccountController extends Controller
{
	@ActionKey("/change_pwd")
	public void change_pwd() throws IOException
	{		
		String old_pwd=this.getPara("old_pwd");
		String new_pwd=this.getPara("new_pwd");
		String confirm_pwd=this.getPara("confirm_pwd");
		
		String student_No=getSessionAttr("student_no");		
		String sql="select student_pwd from student where student_no=?";
		
		
		Record student=Db.findFirst(sql,student_No);
		System.out.println(student);
		String student_pwd=student.getStr("student_pwd");

		if(old_pwd.equals("") || new_pwd.equals("") || confirm_pwd.equals(""))
		{
			setSessionAttr("message","请输入密码");
			redirect("/pages1/stu_password.jsp");
		}
		else if(student_pwd.equals(old_pwd) && new_pwd!="" && confirm_pwd!="")
		{
			if(new_pwd.equals(old_pwd))
			{
				setSessionAttr("message","与原密码相同，无法修改");
				redirect("/pages1/stu_password.jsp");
			}
			else if(new_pwd.equals(confirm_pwd) && !new_pwd.equals(old_pwd))
			{
				sql="update student set student_pwd = ? where student_no=?";
				Db.update(sql,confirm_pwd,student_No);
				setSessionAttr("message","修改密码成功");
				redirect("/pages1/stu_password.jsp");
			}
			else
			{
				setSessionAttr("message","两次输入密码不相同");
				redirect("/pages1/stu_password.jsp");
			}
		}
		
		else if(!student_pwd.equals(old_pwd))
		{
			setSessionAttr("message","原密码输入错误");
			redirect("/pages1/stu_password.jsp");
		}
	}
	
	@ActionKey("/change_information")
	public void change_information()
	{
		String fulltel=this.getPara("longtel");
		String shorttel=this.getPara("shorttel");
		String qq=this.getPara("QQ");
		String email=this.getPara("email");
		
		String student_No=getSessionAttr("student_no");
		String sql=null;
		
		
		if(fulltel!="" && qq!="" && email!="")
		{
			if(shorttel!="")
			{
				sql="update student set student_fulltel=? , student_shorttel=? , " +
						"student_qq=? , student_email=? where student_no=?";
				Db.update(sql,fulltel,shorttel,qq,email,student_No);
				setSessionAttr("message","修改成功");
				redirect("/pages1/stu_information.jsp");
			}
			else
			{
				sql="update student set student_fulltel=? ,student_qq=? ," +
						" student_email=? where student_no=?";
				Db.update(sql,fulltel,qq,email,student_No);
				setSessionAttr("message","修改成功");
				redirect("/pages1/stu_information.jsp");
			}
		}
		else 
		{
			setSessionAttr("message","请输入信息");
			redirect("/pages1/stu_information.jsp");
		}
	}
}
